//
//  GoodDetailWebViewController.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-23.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "GoodDetailWebViewController.h"
#import "CoreDataManager.h"
#import "LFAccount.h"
#import "CONSTS.h"
#import "UrlParameterFactory.h"

@implementation GoodDetailWebViewController

@synthesize webView;
@synthesize num_iid;

- (void)dealloc 
{
    webView.delegate = nil;
    [webView stopLoading];
    LF_RELEASE_SAFELY(webView);
    
    LF_RELEASE_SAFELY(num_iid);
    
    [super dealloc];
}

- (id)initWithNum_iid:(NSString *)num_iid_
{
    self = [super init];
    if (self) 
    {
        self.num_iid = num_iid_;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"商品详情";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"编辑宝贝" 
                                                                              style: UIBarButtonItemStylePlain
                                                                             target: self
                                                                             action: nil] autorelease];
    /*
     由item id拼接wap
     地址，
     可以根据手机淘宝
     的商品详情页自行
     拼出该WAP地址。
     http://
     a.m.taobao.com/
     iitemid.htm
     (注意：拼接规则是
     小写字母 i +
     itemid)
     
     详细解释：
     如果知道item id， 可以根据手机淘宝的商品详情页自行拼出该WAP地址。
     http://a.m.taobao.com/iitemid.htm (注意：拼接规则是小写字母 i + itemid)
     在得到商品的WAP URL后，需要4个重要参数：sid（登陆认证API返回的wap session，长度
     16位；如果用户没有登录，请用小写字母t+imei）、ttid(15位统计标识)、imei(15位机器
     识别码)、imsi(15位SIM卡识别码)，请各位开发者将这四个参数拼在URL后面。
     参考示例：：
     http://a.m.taobao.com/i10539266314.htm?sid=7a1853ecbc1fffb0a273595b55980c19&tt
     id=aaaa&imei=bbbb&imsi=cccc
     */
    
    NSString *goodDetailUrl = [NSString stringWithFormat: @"http://a.m.taobao.com/i%@.htm?", num_iid];
    
    //获取sid
    CoreDataManager *coreDataManager = [CoreDataManager sharedCoreDataManager];
    LFAccount *account = [coreDataManager getCurrentLoggedInAccount];
    
    NSString *sid = account.sid;
    goodDetailUrl = [goodDetailUrl stringByAppendingFormat: @"sid=%@", sid];
    
    goodDetailUrl = [goodDetailUrl stringByAppendingFormat: @"&ttid=%@", ttid];
    
    NSString *imei = getImei();
    goodDetailUrl = [goodDetailUrl stringByAppendingFormat: @"&imei=%@", imei];
    
    NSString *imsi = getImsi();
    goodDetailUrl = [goodDetailUrl stringByAppendingFormat: @"&imsi=%@", imsi];
    
    ITTDINFO(@"%@", goodDetailUrl);
    
    NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString: goodDetailUrl]];
    [webView loadRequest: request];
}

- (void)viewDidUnload
{
    [self setWebView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUDForView: self.view animated: YES];
    [MBProgressHUD showHUDAddedTo: self.view animated: YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUDForView: self.view animated: YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideHUDForView: self.view animated: YES];
}

@end
